package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.UserLike;
import com.itheima.service.mongo.UserLikeService;
import com.itheima.vo.PageBeanVo;
import com.mongodb.client.result.DeleteResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class UserLikeServiceImpl implements UserLikeService {
    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public Long queryEachLikeCount(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        List<Long> likeUserIds = new ArrayList();
        for (UserLike userLike : userLikes) {
            likeUserIds.add(userLike.getLikeUserId());
        }
        query = new Query(Criteria.where("userId").in(likeUserIds).and("likeUserId").is(userId));

        return mongoTemplate.count(query, UserLike.class);
    }

    @Override
    public Long queryLikeCount(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        return mongoTemplate.count(query, UserLike.class);
    }

    @Override
    public Long queryFanCount(Long userId) {
        Query query = new Query(Criteria.where("likeUserId").is(userId));
        return mongoTemplate.count(query, UserLike.class);
    }

    @Override
    public PageBeanVo queryEachLikeList(Long userId, Integer pageNumber, Integer pageSize) {
        Query query = new Query(Criteria.where("userId").is(userId));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);

        List likeUserIds = new ArrayList();
        for (UserLike userLike : userLikes) {
            likeUserIds.add(userLike.getLikeUserId());
        }

        query = new Query(Criteria.where("userId").in(likeUserIds).and("likeUserId").is(userId));

        return this.queryList(query, pageNumber, pageSize);
    }

    @Override
    public PageBeanVo queryLikeList(Long userId, Integer pageNumber, Integer pageSize) {
        Query query = new Query(Criteria.where("userId").is(userId));
        return this.queryList(query, pageNumber, pageSize);
    }

    @Override
    public PageBeanVo queryFanList(Long userId, Integer pageNumber, Integer pageSize) {
        Query query = new Query(Criteria.where("likeUserId").is(userId));
        return this.queryList(query, pageNumber, pageSize);
    }

    @Override
    public PageBeanVo queryList(Query query, Integer page, Integer pageSize) {
        PageRequest pageRequest = PageRequest.of(page - 1, pageSize);
        query.with(pageRequest);
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);

        PageBeanVo pageBeanVo = new PageBeanVo();
        pageBeanVo.setPage(page);
        pageBeanVo.setPagesize(pageSize);
        pageBeanVo.setItems(userLikeList);
        return pageBeanVo;
    }

    @Override
    public Boolean deleteUserLike(Long userId, Long likeUserId) {
        Query query = new Query(Criteria.where("userId").is(userId)
                .and("likeUserId").is(likeUserId));
        DeleteResult remove = mongoTemplate.remove(query, UserLike.class);
        return remove.getDeletedCount() == 1;
    }

    @Override
    public String saveUserLike(Long userId, Long likeUserId) {
        Query query = new Query(Criteria.where("userId").is(userId)
                .and("likeUserId").is(likeUserId));
        if (mongoTemplate.count(query, UserLike.class) > 0) {
            return null;
        }
        UserLike userLike = new UserLike();
        userLike.setId(ObjectId.get());
        userLike.setLikeUserId(likeUserId);
        userLike.setUserId(userId);
        userLike.setCreated(System.currentTimeMillis());

        mongoTemplate.save(userLike);
        return userLike.getId().toHexString();
    }

    @Override
    public Boolean isMutualLike(Long userId, Long likeUserId) {
        Criteria criteria1 = Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId);
        Criteria criteria2 = Criteria.where("userId").is(likeUserId).and("likeUserId").is(userId);

        Criteria criteria = new Criteria().orOperator(criteria1, criteria2);

        return mongoTemplate.count(Query.query(criteria), UserLike.class) == 2;
    }

    @Override
    public UserLike find(Long userId, Long likeUserId) {
        //构造条件
        Query query = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
        UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
        return userLike;
    }
}
